package cn.edu.sgu.www.mhxysy.controller;

import cn.edu.sgu.www.mhxysy.consts.MimeType;
import cn.edu.sgu.www.mhxysy.dto.UserLoginDTO;
import cn.edu.sgu.www.mhxysy.entity.User;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author heyunlin
 * @version 1.0
 */
@RestController
@RequestMapping(path = "/user", produces = MimeType.APPLICATION_JSON_CHARSET_UTF_8)
public class UserController {

	private final UserService userService;

	@Autowired
	public UserController(UserService userService) {
		this.userService = userService;
	}

	@RequestMapping(value = "/logout", method = RequestMethod.POST)
	public JsonResult<Void> logout() {
		userService.logout();

		return JsonResult.success();
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public JsonResult<Void> login(@Validated UserLoginDTO loginDTO) {
		userService.login(loginDTO);

		return JsonResult.success();
	}

	@RequestMapping(value = "/getLogin", method = RequestMethod.GET)
	public JsonResult<User> getLogin() {
		User user = userService.getLogin();

		return JsonResult.success(null, user);
	}

}